//
//  ShareTypeViewController.m
//  LawyerAssistant
//
//  Created by 戴伟 on 2018/11/7.
//  Copyright © 2018年 戴伟. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareBtn1.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>

@interface ShareViewController ()


@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet ShareBtn1 *btn1;
@property (weak, nonatomic) IBOutlet ShareBtn1 *btn2;
@property (weak, nonatomic) IBOutlet ShareBtn1 *btn3;

@end

@implementation ShareViewController


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"000000" andAlpha:0.5];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"000000" andAlpha:0.0];
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(300);
    }];
    
}

- (IBAction)cancel:(id)sender {
    
    [_contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(300);
    }];
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"000000" andAlpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (IBAction)shareType1:(id)sender {
    [self shareWithType:WXSceneSession];
}
- (IBAction)shareType2:(id)sender {
    [self shareWithType:WXSceneTimeline];
}
- (IBAction)shareType3:(id)sender {
    NSString *utf8String = _link;
    NSString *title = _shareTitle;
    NSString *description = _shareDesc;
    NSString *previewImageUrl = @"http://cdni.wired.co.uk/620x413/k_n/NewsForecast%20copy_620x413.jpg";
    QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:utf8String] title:title description:description previewImageURL:[NSURL URLWithString:previewImageUrl]];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
}




-(void)shareWithType:(NSInteger)type{
    WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    message.title = _shareTitle;
    message.description = _shareDesc;
    [message setThumbImage:[UIImage imageNamed:@"appLogo"]];
    // 多媒体消息中包含的图片数据对象
    WXWebpageObject *object = [WXWebpageObject object];
    object.webpageUrl = _link;
    message.mediaObject = object;

    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = type;// 分享到朋友圈
    [WXApi sendReq:req completion:^(BOOL success) {
        if (success) {
            [self cancel:nil];
//            [MBProgressHUD showSuccess:@"分享成功" toView:nil];
        }else{
            [MBProgressHUD showWarning:@"分享出错" toView:nil];
        }
    }];
}


@end
